/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.plugins.impl;

import java.util.ArrayList;
import java.util.List;

import org.grayrabbit.cms.dao.plugins.SurveyDao;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.Survey;
import org.grayrabbit.cms.service.plugins.SurveyService;
import org.grayrabbit.cms.vo.plugins.SurveyBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 16:08:59  
 */
@Service
@Transactional
public class SurveyServiceImpl extends GenericServiceImpl implements SurveyService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("surveyDaoImpl")
	private SurveyDao surveyDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see SurveyService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public Survey getSurvey(final String surveyId){
		Survey survey = surveyDao.findById(surveyId);
		surveyDao.initialize(survey);
		surveyDao.initialize(survey.getSite());
		return survey;
	}
	
	/**
	 * @see SurveyService#delete(String)
	 */
	@Transactional
	public void delete(final Survey survey) {
		surveyDao.makeTransient(survey);
	}
	
	/**
	 * @see SurveyService#save(OperLog)
	 */
	@Transactional
	public void save(final Survey survey) {
		surveyDao.makePersistent(survey);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see SurveyService#getPage(SurveyBean)
	 */
	@Transactional(readOnly=true)
	public Page<Survey> getPage(final SurveyBean surveyBean){
		Page<Survey> page =  new Page<Survey>();
		page.setTotal(surveyDao.count(surveyBean));
		page.setResult(surveyDao.findByQc(surveyDao.assembly(surveyBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see SurveyService#list(Page, SurveyBean)
	 */
	public void list(Page<Survey> page, SurveyBean surveyBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(surveyDao.count(surveyBean));
		}
		page.setResult(surveyDao.findByPage(page,surveyBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see SurveyService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			surveyDao.makeTransient(surveyDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				surveyDao.clear();
		}
		
	}
	/**
	 * 批量启用
	 * @param keys
	 */
	public void enable(String[] keys){
		if(keys!=null){
			for(String str: keys){
				Survey a=surveyDao.findById(str);
				a.setSurveyStatus(true);
				surveyDao.makePersistent(a);
			}
		}
		
	}
	/**
	 * 批量禁用
	 * @param keys
	 */
	public void disable(String[] keys){
		if(keys!=null){
			for(String str:keys){
				Survey a=surveyDao.findById(str);
				a.setSurveyStatus(false);
				surveyDao.makePersistent(a);
			}
		}
	}
	/**
	 * 检查唯一性
	 * @param role
	 * @return
	 */
	public Boolean checkNameUnique(String surveyTitle,String surname,String siteId){
		return surveyDao.checkNameUnique(surveyTitle,surname,siteId);
	}
	
	public List<Survey> listAll(SurveyBean surveyBean){
		return surveyDao.ListAll(surveyBean);
	}
	
	/**
	 * 获取站点下的调查
	 * @param list
	 * @param currentSurveryIndex
	 * @param max
	 * @return
	 */
	public List<String> findSurveyIdsBySiteId(List<String> list,
			int currentSurveryIndex, int max){
		List<String> rs = new ArrayList<String>();
		for(String siteId:list){
			List<Survey> surveyList = surveyDao.findPublishBySiteId(siteId,currentSurveryIndex,max);
			for(Survey survey:surveyList){
				rs.add(survey.getSurveyId());
			}
		}
		return rs;
	}
	
	public List<Survey> surList(){
		return surveyDao.surList();
	}
	/**
	 * 根据站点ID查询
	 * @return
	 */
	public List<Survey>findBySiteId(String siteId){
		return surveyDao.findBySiteId(siteId);
	}
}